import dayjs from "dayjs";
import { usePublicHooks } from "../hooks";
import { reactive, ref, onMounted, h, onBeforeMount } from "vue";
import { message } from "@/utils/message";
import { addDialog } from '@/components/ReDialog'
import { ElMessageBox } from 'element-plus'
import { type FormItemProps } from "./types";
import { type PaginationProps } from "@pureadmin/table";
import elForm from '../form.vue'

import { recordList, excelRacklocations } from '@/utils/api/dispatch/giveAnAlarm'
import { GetAllAgv } from '@/utils/api/dispatch/agv'

export function useDept() {
  const form = reactive({
    code: "",
    content: "",
    recordtype: "",
    agvid: null,
    dateRange: null,
    begin: "",
    end: "",
    pageNumber: 0,
    pageSize: 10,
    sortName: "createtime",
    sortType: 0
  });

  const dataList = ref([]);
  const loading = ref(true);
  const pagination = reactive<PaginationProps>({
    total: 0,
    pageSize: 10,
    currentPage: 1,
    background: true
  });

  // 车辆select
  let agvSelect = ref([])

  const columns: TableColumnList = [
    {
      label: "编码",
      prop: "code",
      width: 150,
      align: "left",
      sortable: true
    },
    {
      label: "车辆名称",
      prop: "agvid",
      minWidth: 100,
      sortable: true
    },
    {
      label: "告警来源",
      prop: "recordtype",
      minWidth: 100,
      sortable: true
    },
    {
      label: "内容",
      prop: "content",
      minWidth: 200,
      sortable: true
    },
    {
      label: "备注",
      prop: "remark",
      minWidth: 100,
      sortable: true
    },
    {
      label: "更新时间",
      prop: "createtime",
      minWidth: 200,
      formatter: ({ createtime }) => dayjs(createtime).format("YYYY-MM-DD HH:mm:ss")
    }
  ]

  async function onSearch() {
    loading.value = true;
    const { Content } = await recordList(form);
    let newData = Content.Results;
    dataList.value = newData;
    pagination.total = Content.TotalCount;
    pagination.pageSize = Content.PageSize;
    pagination.currentPage = Content.PageNumber + 1;

    setTimeout(() => {
      loading.value = false;
    }, 500);
  }

  // 重置
  function resetForm(formEl) {
    if (!formEl) return;
    formEl.resetFields();
    onSearch();
  }

  // 导出
  function exportExcel() {
    ElMessageBox.confirm(
      `是否确认导出数据?`,
      '提示',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
        center: true,
      }
    ).then(() => {
      excelRacklocations(form, '警告消息')
    }).catch(() => { })
  }

  function handleSizeChange(val: number) {
    form.pageSize = val
    onSearch()
  }

  function handleCurrentChange(val: number) {
    form.pageNumber = val - 1
    onSearch()
  }

  onBeforeMount(async () => {
    let { Content: agv } = await GetAllAgv()
    agvSelect.value = agv
  })

  onMounted(() => {
    onSearch()
  })

  return {
    columns,
    loading,
    dataList,
    exportExcel,
    form,
    onSearch,
    resetForm,
    pagination,
    handleSizeChange,
    handleCurrentChange,
    agvSelect
  }
}